package com.xinling.modules.blog.service.impl;

import com.xinling.modules.blog.mapper.BlogPostMapper;
import com.xinling.modules.blog.model.dto.PostDTO;
import com.xinling.modules.blog.model.entity.BlogPost;
import com.xinling.modules.blog.model.vo.PostVO;
import com.xinling.modules.blog.service.BlogPostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlogPostServiceImpl implements BlogPostService {

    @Autowired
    private BlogPostMapper postMapper;

    @Override
    public List<PostVO> getAllPosts() {
        List<BlogPost> posts = postMapper.selectAll();
        return posts.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    @Override
    public PostVO getPostById(Integer id) {
        BlogPost post = postMapper.selectById(id);
        return post != null ? convertToVO(post) : null;
    }

    @Override
    @Transactional
    public PostVO createPost(PostDTO postDTO, Integer authorId) {
        BlogPost post = new BlogPost();
        BeanUtils.copyProperties(postDTO, post);
        post.setAuthorId(authorId);
        post.setViewCount(0);
        post.setLikeCount(0);
        postMapper.insert(post);
        return convertToVO(post);
    }

    @Override
    @Transactional
    public PostVO updatePost(Integer id, PostDTO postDTO) {
        BlogPost post = postMapper.selectById(id);
        if (post != null) {
            BeanUtils.copyProperties(postDTO, post);
            postMapper.updateById(post);
            return convertToVO(post);
        }
        return null;
    }

    @Override
    @Transactional
    public void deletePost(Integer id) {
        postMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void incrementViewCount(Integer id) {
        postMapper.incrementViewCount(id);
    }

    @Override
    @Transactional
    public void incrementLikeCount(Integer id) {
        postMapper.incrementLikeCount(id);
    }

    @Override
    public List<PostVO> searchPosts(String keyword) {
        List<BlogPost> posts = postMapper.searchByKeyword(keyword);
        return posts.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    @Override
    public List<PostVO> getPostsByCategory(Integer categoryId) {
        List<BlogPost> posts = postMapper.selectByCategory(categoryId);
        return posts.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    @Override
    public List<PostVO> getPostsByTag(Integer tagId) {
        List<BlogPost> posts = postMapper.selectByTag(tagId);
        return posts.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    @Override
    public List<PostVO> getPostsByAuthor(Integer authorId) {
        List<BlogPost> posts = postMapper.selectByAuthor(authorId);
        return posts.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    private PostVO convertToVO(BlogPost post) {
        PostVO vo = new PostVO();
        BeanUtils.copyProperties(post, vo);
        return vo;
    }
} 